﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using efuture.http;
using efuture.xml;
using System.IO;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using efuture.utils;


namespace efuture.vdrp
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        private ValidationRuleAdapter vrp = new ValidationRuleAdapter();

        public frmMain()
        {
            InitializeComponent();
            IValidationRule rule = null;

            // RequiredRule
            rule = vrp.Create(textEdit3, ConditionOperator.IsNotBlank);
            rule.ErrorText = "is required";
            vrp.SetValidationRule(rule);

            // 
            rule = vrp.Create(treeList1, ConditionOperator.IsNotBlank, new TreeListColumn[] { treeListColumn1, treeListColumn3 });
            rule.ErrorText = "column is not allow blank ";
            vrp.SetValidationRule(rule);

            HttpHelper.serverURL = "http://192.168.1.101:8090/efuture_studio/rest";

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            String URL = "/sys/role/find";

            String response = HttpHelper.DoGet(URL, null);
            XMLHelper.BindingData(panelControl1, new StringReader(response), "/Root");
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (!this.vrp.Validate()) {
                return;
            }

            String URL = "/sys/role/add";
            String xmlData = XMLHelper.encode(panelControl2, "Role");
            //MessageBox.Show(xmlData);
            String response = HttpHelper.DoPost(URL, xmlData, null);
            //MessageBox.Show(response);
            if (XMLHelper.ShowException(response, true))
            {
                return;
            }

            this.loadData();

        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {

            Dictionary<String, String> d = new Dictionary<String, String>();
            d.Add("to", "wangpa@e-future.com.cn");
            d.Add("subject", "测试主题");
            d.Add("text", "text测试内容");
            HttpHelper.DoGet("/mail/send/text", d);

            d = new Dictionary<String, String>();
            d.Add("to", "wangpa@e-future.com.cn");
            d.Add("subject", "测试主题");
            d.Add("html", "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><body>html测试内容</body></html>");
            HttpHelper.DoGet("/mail/send/html", d);

        }
    }

}